<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>后台登录</title>
  <link rel="stylesheet" href="/static/base.css">
  <link rel="stylesheet" href="/static/components.css">
  <link rel="stylesheet" href="/static/pages.css">
  <style>
    .admin-login { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
    .admin-card { width: 360px; background: #fff; padding: 24px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.08); }
    .admin-card h1 { font-size: 20px; margin: 0 0 16px; }
    .form-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
    .form-row input { height: 38px; padding: 8px 10px; border: 1px solid #ced4da; border-radius: 8px; }
    .btn { height: 40px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
    .btn-primary { background: linear-gradient(135deg,#667eea,#764ba2); color: #fff; width: 100%; }
    .error { color: #d93025; margin-top: 8px; min-height: 20px; }
  </style>
</head>
<body>
  <div class="admin-login">
    <div class="admin-card">
      <h1>后台登录</h1>
      <div class="form-row">
        <label>用户名</label>
        <input id="username" autocomplete="username" placeholder="输入用户名">
      </div>
      <div class="form-row">
        <label>密码</label>
        <input id="password" type="password" autocomplete="current-password" placeholder="输入密码">
      </div>
      <button id="loginBtn" class="btn btn-primary">登录</button>
      <div id="error" class="error"></div>
    </div>
  </div>

  <script>
    async function login(){
      const username = document.getElementById('username').value.trim();
      const password = document.getElementById('password').value.trim();
      const err = document.getElementById('error');
      err.textContent='';
      try{
        const resp = await fetch('/admin/api/login', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({username, password})});
        const data = await resp.json();
        if(data.success){
          location.href = '/admin';
        }else{
          err.textContent = data.error || '登录失败';
        }
      }catch(e){ err.textContent = e.message || '网络错误'; }
    }
    document.getElementById('loginBtn').addEventListener('click', login);
    document.addEventListener('keypress', e=>{ if(e.key==='Enter') login();});
  </script>
</body>
</html>
